﻿using AngleExpress.utils;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;

namespace AngleExpress.software
{
    class BaiShiExpress:ISoftware
    {
        private string ExePath = System.AppDomain.CurrentDomain.BaseDirectory + @"bs\Express.exe";
    
        public override void OpenSoftware(string username,string password,string otherParam)
        {
            ProcessStartInfo info = new ProcessStartInfo();
            info.FileName = ExePath;
            info.UseShellExecute = true;
            info.Arguments = "";
            try { 
                Process pro = Process.Start(info);
            }
            catch (Exception e)
            {
                throw e;
            }
            WaitInit();
            InputNew(username,password);
        }

        private void WaitInit()
        {
            while (true)
            {
                IntPtr mainWindow = WinApi.FindWindow(null, "登录-百世快递管理系统");
                if (mainWindow == IntPtr.Zero)
                {
                    Console.WriteLine("百世主窗口未启动");
                    Thread.Sleep(100);
                    continue;
                }
                var tabButtons = FindTabButton(mainWindow);
                if (tabButtons.Count == 0)
                {
                    Console.WriteLine("百世tabbutton未找到");
                    Thread.Sleep(100);
                    continue;
                }
                //IntPtr lparam = (IntPtr)((230 << 16) | 740);
                //GCHandle handle = GCHandle.Alloc(param);
                //var lparam = GCHandle.ToIntPtr(handle);
                //Console.WriteLine(WinApi.SendMessage(mainWindow, Message.WM_LBUTTONDOWN, (IntPtr)1, lparam));
                //Console.WriteLine(WinApi.SendMessage(mainWindow, Message.WM_LBUTTONUP, (IntPtr)1, lparam));
                //if (handle.IsAllocated)
                //    handle.Free();
                break;

            }
        }

        private void InputNew(string username, string password)
        {
            while (true)
            {

                IntPtr maindHwnd = WinApi.FindWindow(null, "登录-百世快递管理系统");
                if (maindHwnd != IntPtr.Zero)
                {
                    var editHandles = FindEditBox(maindHwnd);
                    if (editHandles.Count() > 1)
                    {
                        WinApi.SendMessage(editHandles[0], Message.WM_SETTEXT, IntPtr.Zero, Marshal.StringToHGlobalAnsi(password));
                        WinApi.SendMessage(editHandles[2], Message.WM_SETTEXT, IntPtr.Zero, Marshal.StringToHGlobalAnsi(username));
                        break;
                    }
                }

                TextLog.Add("not find baishi");
                Thread.Sleep(500);
            }
        }



        private List<IntPtr> FindEditBox(IntPtr parent)
        {
            return GetChildWindows(parent, EnumEditBox);
        }

        private List<IntPtr> FindTabButton(IntPtr parent)
        {
            return GetChildWindows(parent, EnumTabButton);
        }

        private List<IntPtr> GetChildWindows(IntPtr parent,WinApi.EnumWindowProc enumProc)
        {
            var list = new List<IntPtr>();
            var listHandle = GCHandle.Alloc(list);

            try
            {
                WinApi.EnumChildWindows(parent, enumProc, GCHandle.ToIntPtr(listHandle));
            }
            finally
            {
                Console.WriteLine("Finally");
                // Free the list handle.
                if (listHandle.IsAllocated)
                    listHandle.Free();
            }
            return list;
        }

        private bool EnumTabButton(IntPtr handle, IntPtr pointer)
        {
            var gcHandle = GCHandle.FromIntPtr(pointer);

            var list = gcHandle.Target as List<IntPtr>;

            var sb = new StringBuilder(100);
            WinApi.GetWindowText(handle, sb, 100);
            if (sb.ToString()== "账号登录")
            {
                list.Add(handle);
            }

            return true;
        }

        private bool EnumEditBox(IntPtr handle, IntPtr pointer) {
            var gcHandle = GCHandle.FromIntPtr(pointer);

            var list = gcHandle.Target as List<IntPtr>;

            var sb = new StringBuilder(100);
            WinApi.GetClassName(handle, sb, 100);
            if (sb.ToString() == "WindowsForms10.EDIT.app.0.13965fa_r9_ad1")
            {
                list.Add(handle);
            }

            return true;
        }
    }
}
